/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.user;

import sw6.digipecs.R;
import sw6.digipecs.helpers.AudioPlayer;
import sw6.digipecs.helpers.DigiLoader;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.widget.AnimationLayout;
import sw6.digipecs.widget.PageBarLayout;
import sw6.digipecs.widget.dragdrop.DragLayout;
import sw6.digipecs.widget.user.PECSBookPageView;
import sw6.digipecs.widget.user.SentenceBoardLayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 *  Main entrance point for digiPECS, this activity shows the book and allows
 *  creation of sentences.
 */
public class MainActivity extends GirafActivityPatch {
	
	private PECSBook mBook;
	private SentenceBoardLayout mSentenceBoard;
	private PageBarLayout mPagebar;
	private static boolean mRunning;
	private static Activity mActivity;
	
	public static boolean isRunning() {
		return mRunning;
	}
	
	public static void setRunning(boolean b) {
		mRunning = b;
	}
	
	public static Activity getActivity() {
		return mActivity;
	}
	
	/**
	 *  Called, by Android, when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		mActivity = this;
		
		// Get widgets we need
		PECSBookPageView pageView = (PECSBookPageView) findViewById(R.id.mainImgGrid);
		DragLayout dragLayout = (DragLayout) findViewById(R.id.mainDrag);
		AnimationLayout animationLayout = (AnimationLayout) findViewById(R.id.mainAnimation);
		mSentenceBoard = (SentenceBoardLayout) findViewById(R.id.sentenceboardView);
		mPagebar = (PageBarLayout) findViewById(R.id.pagebar);
		
		// Create book
		mBook = new PECSBook(this, PECSBook.Views.ActiveOnlyInSituation);
		
		// Set up things
		pageView.setUp(mBook, dragLayout, mPagebar);
		mPagebar.setBook(mBook);
		mSentenceBoard.setDragLayer(dragLayout);
		mSentenceBoard.setAnimationLayer(animationLayout);
		pageView.setAnimationLayer(animationLayout);
		
		//TODO Shake it
		/*final SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
		final Sensor sensor = sensorMgr.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER);
		sensorMgr.registerListener(new ShakeDetecter(this), sensor, SensorManager.SENSOR_DELAY_GAME);*/
	
		//TODO test code, cleaning!
		DigiLoader.Load(mActivity, mActivity);
		
		/*final Activity thisactivity = this;
		Button button = new Button(this);
		button.setText("Push me");
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				thisactivity.startActivity(new Intent(thisactivity, SelectActiveSituationActivity.class));
			}
		});
		animationLayout.addView(button);*/
	}

	/**
	 *  Called, by Android, when the activity is paused.
	 *  
	 *  This closes the book, which is then reopened by onResume(). 
	 */
	@Override
	protected void onPause() {
		mBook.close();
		AudioPlayer.close();
		mSentenceBoard.saveState();
		
		super.onPause();
	}

	/**
	 *  Called, by Android, when the activity is resumed.
	 *  
	 *  This opens the book, which may have never been open, or was previously
	 *  closed by onPause().
	 */
	@Override
	protected void onResume() {
		mRunning = true;
		mBook.open();
		AudioPlayer.open();
		mSentenceBoard.restoreState();
		
		super.onResume();
	}

	// Save and restore states (Had problems doing these in the PageBarLayout):
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		mBook.setPage(savedInstanceState.getInt("PecsBookPage"));
		
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt("PecsBookPage", mBook.getPage());
		
		super.onSaveInstanceState(outState);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.pecsmenu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.playall:
	        mSentenceBoard.playAll();
	        return true;
	    case R.id.clearall:
	        mSentenceBoard.clearAll();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
}